package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.feedback.WXFeedbackSubmit;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketFeedback;
import com.cskaoyan.bean.po.MarketFeedbackExample;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.usersadmin.FeedbackData;
import com.cskaoyan.mapper.MarketFeedbackMapper;
import com.cskaoyan.service.BrandService;
import com.cskaoyan.service.FeedbackService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * ClassName: FeedbackServiceImpl
 * Description:
 * date: 2022/6/5 21:43
 *
 * @author bruce
 * @since JDK 8
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;

    @Override
    public FeedbackData query(BaseParam param, String username, Integer id) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketFeedbackExample example = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = example.createCriteria();
        //添加条件
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        criteria.andDeletedEqualTo(false);
        //排序规则
        example.setOrderByClause("add_time desc");
        List<MarketFeedback> marketFeedbacks = marketFeedbackMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketFeedbacks);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return FeedbackData.data(total, pages, param.getLimit(), param.getPage(), marketFeedbacks);
    }

    //评论
    @Override
    public void insertSubmit(WXFeedbackSubmit wxFeedbackSubmit) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = null;
        if (subject.isAuthenticated()) {
            primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        }

            MarketFeedback marketFeedback = new MarketFeedback();
            marketFeedback.setId(null);
            marketFeedback.setUserId(primaryPrincipal.getId());
            marketFeedback.setUsername(primaryPrincipal.getUsername());
            marketFeedback.setMobile(wxFeedbackSubmit.getMobile());
            marketFeedback.setFeedType(wxFeedbackSubmit.getFeedType());
            marketFeedback.setContent(wxFeedbackSubmit.getContent());
            marketFeedback.setStatus(Integer.valueOf(primaryPrincipal.getStatus()));
            marketFeedback.setHasPicture(wxFeedbackSubmit.isHasPicture());
            marketFeedback.setPicUrls(wxFeedbackSubmit.getPicUrls());
            Date date = new Date();
            marketFeedback.setAddTime(date);
            marketFeedback.setUpdateTime(date);
            marketFeedback.setDeleted(false);
            marketFeedbackMapper.insertSelective(marketFeedback);

        }
    }
